//
//  ContactForUsViewController.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/4/26.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "ContactForUsViewController.h"
#import "UserInfoTableViewCell.h"
#define UserCellId @"cellid1"
@interface ContactForUsViewController ()<UIAlertViewDelegate>
@property (nonatomic,strong) NSMutableArray *titleArray;
@end

@implementation ContactForUsViewController

#pragma mark - Life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self resetNav];
    [self setWebData];
    [self.titleArray addObjectsFromArray:@[@"客服电话",@"公司邮箱",@"官方网站"]];
}

#pragma mark - Property
- (NSMutableArray *)titleArray{
    if (_titleArray==nil) {
        _titleArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _titleArray;
}
#pragma mark - UI
- (void)resetNav{
    [self setTheBackBtn];
    self.myTitle.text = @"联系我们";
    [self.tableView registerClass:[UserInfoTableViewCell class] forCellReuseIdentifier:UserCellId];
}
#pragma mark - UI Response

#pragma mark - Data handle
- (void)setWebData{
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [NetWorkTool getCallMeblock:^(id data) {
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
        if (data) {
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:data[@"data"]];
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，就检查网络" animated:YES];
        }
    }];
}
#pragma mark - Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50*HeightRate;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        
        if (IsPad) {
            
        }else{
        UserInfoTableViewCell *cell = (UserInfoTableViewCell *)[self tableView:self.tableView cellForRowAtIndexPath:indexPath];
        NSString *str = cell.contentLab.text;
        NSMutableString* str1=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",str];
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str1]];
        }
    
    }
    if (indexPath.row == 1){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"是否给此邮箱发邮件？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
       
       
    }
    if (indexPath.row==2) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"是否前往官方网站？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
       
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UserInfoTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:UserCellId];
    cell.titleLab.text = self.titleArray[indexPath.row];
    switch (indexPath.row) {
    case 0:
        {
            for (NSDictionary *dic in self.dataArray) {
                if ([dic[@"paramCode"]isEqualToString:@"SERVICE_TEL"]) {
                    cell.contentLab.text = dic[@"paramValue"];
                }
            }
        }
        break;
    case 1:
        {
            for (NSDictionary *dic in self.dataArray) {
                if ([dic[@"paramCode"]isEqualToString:@"E_MAIL"]) {
                    cell.contentLab.text = dic[@"paramValue"];
                }
            }
        }
        break;
    case 2:
        {
            for (NSDictionary *dic in self.dataArray) {
                if ([dic[@"paramCode"]isEqualToString:@"WEB_SITE"]) {
                    cell.contentLab.text = dic[@"paramValue"];
                }
            }
        }
        break;
   
    default:
       
            break;
        
    }
    return  cell;
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView.title isEqualToString:@"是否给此邮箱发邮件？"]) {
        if (buttonIndex) {
            UserInfoTableViewCell *cell = (UserInfoTableViewCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
            NSString *str = cell.contentLab.text;
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto://%@",str ]]];
        }
    }else{
        if (buttonIndex) {
            UserInfoTableViewCell *cell = (UserInfoTableViewCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
            NSString *str = cell.contentLab.text;
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
    }
}

@end
